<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title>加减班</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../common/mui/mui.css" rel="stylesheet" />
		<link href="../../common/css/app.css" rel="stylesheet" />
		<link href="../../common/mui/mui.picker.min.css" rel="stylesheet" />
		<style type="text/css">
			.mui-input-row,.mui-table-view{
				background-color: white;
				margin-top: var(--size10)!important;
				font-size: var(--size14);
			}
			.mui-input-row .textTip{
				float: right;
				height: 40px;
				line-height: 40px;
			}
			.mui-input-row label{
				height: 40px;
				line-height: 40px;
				padding: 0 var(--size15);
			}
			.mui-input-row .mui-switch{
				margin: var(--size5) var(--size10);
			}
			
			.mui-input-row textarea{
				width: 100%;
				float: none;
				border: none;
				margin: 0;
				font-size: var(--size14);
			}
			.mui-input-row .length{
				position: absolute;
				right: var(--size10);
				left: auto;
				bottom: var(--size10);
				color: gray;
			}
			.mui-table-view:before,.mui-table-view:after{
				height: 0;
			}
			.mui-table-view-cell{
				padding-right: 0;
				position: relative;
				padding-right: var(--size32);
			}
			.mui-table-view-cell.mui-active {
			    background-color: white;
			}
			.mui-table-view-cell:last-child{
				padding-right: var(--size15);
			}
			.textTip{
				float: left;
			}
			.textTip.mui-active{
				color: green;
			}
			.textContent{
				float: right;
				color: gray;
			}
			.angleRight{
				position: absolute;
				right: 0;
				padding-right: var(--size15);
				padding-left: var(--size50);
			}
			.angleRight img{
				width: var(--size7);
			}
			.mui-scroll-wrapper{
				bottom: var(--size50);
			}
			.bottomArea{
				height: var(--size50);
				position: absolute;
				top: auto;
				bottom: 0;
				left: 0;
				width: 100%;
			}
			#deleteBtn,#saveBtn{
				height: 100%;
				width: 50%;
				background-color: var(--mainColor);
				margin: 0;
				padding: 0;
				font-size: var(--size14);
				line-height: var(--size50);
				float: left;
				border: none;
				border-radius: 0;
			}
			#deleteBtn{
				background-color: white;
				color: orangered;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">加减班</h1>
		</header>
		<div class="mui-content mui-scroll-wrapper">
			<div class="mui-scroll">
				<div class="mui-input-row" style="padding-right: var(--size15);">
					<label>类型</label>
					<div id="plusTip" class="textTip mui-active">加班</div>
					<div id="switch" class="mui-switch mui-switch-mini mui-active">
					    <div class="mui-switch-handle"></div>
					</div>
					<div class="textTip">减班</div>
				</div>
				<ul class="mui-table-view">
					<li class="mui-table-view-cell">
						<div class="textTip">开始时间</div>
						<div class="textContent" id="stTime">请选择</div>
						<div class="angleRight"><img src="../../dependence/teach/images/arrowR.svg"/></div>
					</li>
					<li class="mui-table-view-cell">
						<div class="textTip">结束时间</div>
						<div class="textContent" id="endTime">请选择</div>
						<div class="angleRight"><img src="../../dependence/teach/images/arrowR.svg"/></div>
					</li>
					<li class="mui-table-view-cell">
						<div class="textTip">时长</div>
						<div id="timeLen" class="textContent"></div>
					</li>
				</ul>
				<div class="mui-input-row">
					<textarea id="mark" rows="5" placeholder="请输入备注"></textarea>
					<div class="length"><span>0</span>/50</div>
				</div>
			</div>
		</div>
		<div class="bottomArea">
			<div id="deleteBtn" class="mui-btn mui-btn-primary">删除</div>
			<div id="saveBtn" class="mui-btn mui-btn-primary">保存</div>
		</div>
		<script src="../../common/mui/mui.js"></script>
		<script src="../../common/mui/mui.picker.min.js"></script>
		<script src="../../common/js/jquery-3.4.1.min.js"></script>
		<script src="../../common/js/globalSet.js"></script>
		<script src="../../common/js/DHCWebService.js"></script>
		
		<script type="text/javascript">
		var nurseid='', sourceData
			mui.init()
			mui.plusReady(function () {
			    baseSetup();
			})
			if (!mui.os.plus) {
				baseSetup()
			}
			/*parr1	排班表信息    状态：N 草稿 S 保存 R 提交 A 审核 B 驳回
			* ArgWardDR|病区Id^ArgStDate|开始日期^ArgEndDate|结束日期^ArgCrtUser|创建者user Id^ArgRemarks|排班表备注^ArgStatus|状态^rw|排班表Id^WardDesc|病区描述
			* parr2	排班行信息	ArgPerDR|护士person Id^ArgPerSort|行序号^ArgGroupDR|分组Id^ArgEditUser|操作人user Id^ArgWorkRemark|行备注^rw|行Id^ArgPerBeds|负责床位
			* parr3	排班详情	 ArgType|加减班类型^ArgHolidayTime|休假时间^ArgPerDR|护士person Id^ArgDate|排班日期^ArgPostDR|病区班次Id^ArgStTime1|一段开始时间^ArgEndTime1|一段结束时间^ArgStTime2|二段开始时间^ArgEndTime2|二段结束时间^ArgStTime3|三段开始时间^ArgEndTime3|三段结束时间^ArgWorkRemark|班次备注^rw|排班Id^ArgSecStTime|二线班开始时间^ArgSecEndTime|二线班结束时间^ArgEditUser|操作人user Id
			* 加减班类型：Y加班N减班 休假时间：H全天 A上午 P下午
			*/
			function saveOnePersonSchInfo(argInfo,locSchArr,index){
				var ArgType = "Y"
				if (!document.querySelector('#switch.mui-active')) {
					ArgType = "N"
				}
				var perInfo = locSchArr[index]
				var ArgCrtUser = argInfo['ArgCrtUser'];
				var ArgStatus = argInfo['ArgStatus']
				var rw = argInfo['rw']
				var beds = perInfo['ArgPerBeds']!=undefined?perInfo['ArgPerBeds']:""
				var parr1 = 'ArgWardDR|'+sourceData['wardId']+'^ArgStDate|'+argInfo['ArgStDate']+'^ArgEndDate|'+argInfo['ArgEndDate']+'^ArgCrtUser|'+ArgCrtUser+
					'^ArgRemarks|'+argInfo['ArgRemarks']+'^ArgStatus|'+ArgStatus+'^rw|'+argInfo['rw']+'^WardDesc|'+argInfo['WardDesc']
				var parr2 = 'ArgPerDR|'+perInfo['ArgPerDR']+'^ArgPerSort|'+perInfo['ArgPerSort']+'^ArgGroupDR|'+perInfo['ArgGroupDR']+
					'^ArgEditUser|^ArgWorkRemark|^rw|'+perInfo['ArgRowId']+'^ArgPerBeds|'+beds
				
				var ArgWorkRemark = '' ;//班次备注
				var parr3 = 'ArgType|'+ArgType+'^ArgHolidayTime|^ArgPerDR|'+perInfo['ArgPerDR']+'^ArgDate|'+sourceData['date']+
					'^ArgPostDR|^ArgStTime1|'+mui('#stTime')[0].innerText+'^ArgEndTime1|'+mui('#endTime')[0].innerText+
					'^ArgStTime2|^ArgEndTime2|^ArgStTime3|^ArgEndTime3|^ArgWorkRemark|'+mui('#mark')[0].value+'^rw|^ArgSecStTime|^ArgSecEndTime|^ArgEditUser|'+nurseid
				var pdict = {'parr1':parr1,'parr2':parr2,'parr3':parr3}
				//console.log(JSON.stringify(pdict))
				var paramDict = {'method':'web.INMArgComm:SaveArgSub','params':JSON.stringify(pdict),'type':'Method'}
				mui.DHCWebService(paramDict, function(result) {
					//console.log(JSON.stringify(result))
					var msg = parseInt(result['msg'])
					if (!isNaN(msg) && msg > 0) {//保存成功
						var schLocPage = plus.webview.getWebviewById('nurSchLoc')
						if (schLocPage) {
							schLocPage.evalJS('refreshPageData("sch")')
						}
						mui.back()
					}else{
						mui.toast(result['msg'])
					}
				}, function(errorStr) {
					mui.toast('请重试')
				});
			}
			function deleteBtnTapped(){
				mui('textarea').each(function (index,dom) {
					dom.value = ''
					dom.nextElementSibling.querySelector('span').innerHTML = '0'
				})
			}
			function saveBtnTapped(){
				if (mui('#stTime')[0].innerText == '请选择') {
					mui.toast('请选择开始时间')
					return
				}
				if (mui('#endTime')[0].innerText == '请选择') {
					mui.toast('请选择开始时间')
					return
				}
				if (sourceData['rw'] != "") {
					saveOnePersonSchInfo(sourceData['argInfo'],sourceData['locSchArr'],sourceData['index'])
					return
				}
				var schLocPage = plus.webview.getWebviewById('nurSchLoc')
				if (schLocPage) {
					schLocPage.evalJS('refreshPageData("create","nurSchPlus","saveOnePersonSchInfo")')
				}
			}
			function selectTime() {
				var _self = this
				_self.classList.add('mui-active')
				if(_self.picker) {
					_self.picker.show(function(rs) {
						_self.previousElementSibling.innerText = rs.text
						computeTimeLength()
						_self.classList.remove('mui-active')
						_self.picker.dispose();
						_self.picker = null;
						
					});
				} else {
					var optionsJson = '{"type":"time"}';
					var options = JSON.parse(optionsJson);
					_self.picker = new mui.DtPicker(options);
					_self.picker.show(function(rs) {
						_self.previousElementSibling.innerText = rs.text
						computeTimeLength()
						_self.classList.remove('mui-active')
						_self.picker.dispose();
						_self.picker = null;
					});
				}
			}
			function computeTimeLength(idStr){
				var stTimeE = document.getElementById('stTime');
				var stTime = stTimeE.innerText;
				var endTimeE = document.getElementById('endTime');
				var endTime = endTimeE.innerText;
				var timeLen = document.getElementById('timeLen');
				if (stTime == '请选择' || endTime == '请选择') {
					timeLen.innerText = '0小时';
					return;
				}
				if (stTime > endTime) {
					mui.toast('结束日期不能早于开始日期');
					timeLen.innerText = '0小时';
					return;
				}
				var stDict = getHourMinute(stTime);
				var endDict = getHourMinute(endTime);
				var hourLen = endDict['hour'] - stDict['hour'];
				var minuteLen = endDict['minute'] - stDict['minute']
				if (minuteLen == 0) {
					timeLen.innerText = hourLen + '小时';
					return;
				}else if (minuteLen < 0) {
					hourLen -= 1;
					minuteLen += 60;
				}
				var destFloat = hourLen + (minuteLen / 60.0);
				var destStr = getOneSmallNumberStr(destFloat);
				timeLen.innerText = destStr + '小时';
			}
			
			function getHourMinute(timeStr){
				var timeArr = timeStr.split(':');
				var hourStr = timeArr[0];
				if (hourStr.indexOf('0') == 0) {
					hourStr = hourStr.substring(1,hourStr.length);
				}
				var hour = parseInt(hourStr);
				var minuteStr = timeArr[1];
				if (minuteStr.indexOf('0') == 0) {
					minuteStr = minuteStr.substring(1,minuteStr.length);
				}
				var minute = parseInt(minuteStr);
				return {'hour':hour,'minute':minute};
			}
			
			function getOneSmallNumberStr(numFloat){
				var numStr = numFloat + '';
				var dropIndex = numStr.indexOf('.');
				if (numStr.length > dropIndex+2) {
					var intPart = parseInt(numStr.substr(0,dropIndex));
					var first = parseInt(numStr.substr(dropIndex+1,1));
					var second = parseInt(numStr.substr(dropIndex+2,1));
					if (second < 5) {
						if (first == 0) {
							return intPart + '';
						}
						numStr = numStr.substring(0,dropIndex+2);
						return numStr;
					}
					first += 1;
					if (first > 9 ) {
						numStr = (intPart+1) + '';
					}else{
						numStr = intPart + '.'+first;
					}
				}
				return numStr;
			}
			

			function baseSetup(){
				mui('.mui-scroll-wrapper').scroll({
					bounce: true,
					indicators: false, //是否显示滚动条
				});
				var self = plus.webview.currentWebview();
				sourceData = self.sourceData;//{'wardId':wardID,'sch':schStr,'date':dateStr,'ArgPerDR':ArgPerDR,'ArgRowId':ArgRowId,rw:argInfo['rw'],'PerName':}
				nurseid = sourceData['nurseid']
				mui(".mui-scroll").on("input",'textarea', function() {
					var textStr = this.value
					if (textStr.length > 50) {
						this.value = textStr.substring(0,50)
					}
					var countDom = this.nextElementSibling.querySelector('span')
					countDom.innerHTML = this.value.length
				});
				mui('.mui-table-view').on('tap', '.angleRight', selectTime);
				mui('body').on('tap','.mui-dtpicker-header > button',function(e){
					var activeDom = document.body.querySelector('.angleRight.mui-active')
					if (activeDom) {
						activeDom.classList.remove('mui-active')
					}
				});
				mui("body").on("tap",".mui-backdrop",function(){
					var activeDom = document.body.querySelector('.angleRight.mui-active')
					if (activeDom) {
						activeDom.classList.remove('mui-active')
					}
				})
				mui('#deleteBtn')[0].addEventListener('tap',deleteBtnTapped)
				mui('#saveBtn')[0].addEventListener('tap',saveBtnTapped)
				mui('#switch')[0].addEventListener('toggle', function(){
					if (this.getAttribute('class').indexOf('mui-active') != -1) {
						mui('#plusTip')[0].classList.add('mui-active')
					}else{
						mui('#plusTip')[0].classList.remove('mui-active')
					}
				});
			}
		</script>
	</body>

</html>
